<?php

namespace App\Models\Traits;


use App\Http\Middleware\OpenShop\MerchantAppAuth;

trait KfUid
{
    public static function bootKfUid()
    {
        static::saving(function ($model) {
            try {
                $operateUid = (int)optional(getRmsUserInfoByAuth())->id;

                if (!$operateUid) {
                    $operateUid = MerchantAppAuth::$userId;
                }

                if (!$operateUid) {
                    $operateUid = auth()->user()->rmsUserId ?? 0;
                }

            } catch (\Exception $e) {
                $operateUid = 0;
            }

            if (!$model->exists) {
                $model->setAttribute('createdByUid', $operateUid);
            }
            $model->setAttribute('updatedByUid', $operateUid);
        });
    }
}
